/**
 * 
 */
package com.milk_shop.action;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.milk_shop.model.Package;
import com.milk_shop.model.ShoppingCart;
import com.milk_shop.model.Food;
import com.milk_shop.service.FoodService;
import com.milk_shop.service.PackageService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("prototype")
public class OrderAction extends ActionSupport {
	FoodService foodService;
	PackageService packageService;
	private int foodType;
	private int foodId;
	private int packageId;
	
	public FoodService getFoodService() {
		return this.foodService;
	}
	@Resource
	public void setFoodService(FoodService foodService) {
		this.foodService = foodService;
	}

	public PackageService getPackageService() {
		return this.packageService;
	}
	@Resource
	public void setPackageService(PackageService packageService) {
		this.packageService = packageService;
	}
	
	public int getFoodType() {
		return this.foodType;
	}
	
	public void setFoodType(int foodType) {
		this.foodType = foodType;
	}
	
	public int getFoodId() {
		return this.foodId;
	}
	
	public void setFoodId(int foodId) {
		this.foodId = foodId;
	}
	
	public int getPackageId() {
		return this.packageId;
	}
	public void setPackageId(int packageId) {
		this.packageId = packageId;
	}
	public String getAll() {
		try {
			List<Food> foods = foodService.getAll();
			if(foods==null)throw new Exception("food is null");
			ActionContext.getContext().put("foods", foods);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String getFood() {
		try {
			List<Food> foods = foodService.getFoodByType(foodType);
			if(foods==null)throw new Exception("food is null");
			ActionContext.getContext().put("foods", foods);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	
	public String getRecommend() {
		try {
			List<Food> foods = foodService.getReCommendFood();
			if(foods==null)throw new Exception("food is null");
			ActionContext.getContext().put("foods", foods);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	
	public String getDesc() {
		try {
			List<Food> foods = foodService.getFoodBySalesDesc();
			if(foods==null)throw new Exception("food is null");
			ActionContext.getContext().put("foods", foods);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String getPackage() {
		try {
			List<Package> packages = packageService.getAll();
			if(packages==null)throw new Exception("packages is null");
			ActionContext.getContext().put("packages", packages);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String getDetail() {
		try {
			Food food=foodService.getModelById(foodId);			
			if(food==null)throw new Exception("food is null");
			System.out.println(food.getName());
			ActionContext.getContext().put("food", food);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	
	public String getPackageDetail() {
		try {
			Package package1=packageService.getModelById(packageId);
			if(package1==null)throw new Exception("package is null");
			System.out.println(package1.getName());
			ActionContext.getContext().put("package", package1);
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String addFood() {
		try {
			Food food=foodService.getModelById(foodId);			
			if(food==null)throw new Exception("food is null");
			ShoppingCart.getInstance().getFoods().add(food);
			return getAll();
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	public String addPackage() {
		try {
			Package package1=packageService.getModelById(packageId);
			if(package1==null)throw new Exception("package is null");
			ShoppingCart.getInstance().getPackages().add(package1);
			return getPackage();
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
}
